package com.example.controller;

import com.example.entity.Rest;
import com.example.utils.WebUtils;
import com.xiaoleilu.hutool.io.FileUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.nio.charset.Charset;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * @Desc:
 * @Author zzz
 * @Date 2021/2/25
 * @Version V1.0
 */
@Slf4j
@Api(tags = "TestController", description = "Just For Test")
@RequestMapping("/api")
@RestController
public class TestController {


    @ApiOperation("Hello Test")
    @GetMapping("/hello")
    public String hello() {
        return "Hello World";
    }


    @ApiOperation(value = "Post Request Test", produces = "application/json;charset=utf-8")
    @PostMapping("/test")
    public Rest test() {
        return Rest.ok("Hello World");
    }

    @ApiOperation("单上传文件")
    @PostMapping(value = "/uploadData", consumes = "multipart/form-data", produces = "application/json;charset=utf-8")
    @ApiImplicitParams({@ApiImplicitParam(name = "file", value = "文件流对象", required = true, dataType = "MultipartFile")})
    public Rest uploadData(MultipartFile file) {
        if (file == null) {
            return Rest.error("请选择要上传的文件");
        }
        log.debug("收到文件:{}", file.getOriginalFilename());
        return Rest.ok(null, "上传成功");
    }

    @ApiOperation("多上传文件")
    @PostMapping(value = "/batchUploadData", consumes = "multipart/form-data", produces = "application/json;charset=utf-8")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "file", value = "文件流对象", required = true, dataType = "MultipartFile", allowMultiple = true)
    })
    public Rest batchUploadData(MultipartFile[] file) {
        if (file.length == 0) {
            return Rest.error("请选择要上传的文件");
        }
        for (MultipartFile multipartFile : file) {
            log.debug("收到文件:{}", multipartFile.getOriginalFilename());
        }
        return Rest.ok(null, "上传成功");
    }


    @ApiOperation(value = "下载测试", produces = "application/octet-stream")
    @GetMapping("/download")
    public void testDownload(HttpServletResponse response) {
        File file = new File("/tmp/download/test.txt");
        StringBuffer sb = new StringBuffer("down file test ");
        DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-hh HH:mm:ssS");
        sb.append(df.format(LocalDateTime.now()));
        FileUtil.writeString(sb.toString(), file, Charset.defaultCharset());
        for (String line : FileUtil.readLines(file, Charset.defaultCharset())) {
            log.debug(line);
        }
        WebUtils.downloadConfig(response, file);
    }

}
